package com.itheima.controller;


import com.itheima.common.JsonResult;
import com.itheima.common.SnowFlake;
import com.itheima.pojo.admin.Pagination;
import com.itheima.pojo.client.OrderAddDto;
import com.itheima.pojo.client.OrderPagingVo;
import com.itheima.pojo.client.orderPaginationVo.OrderPaginationVo;
import com.itheima.service.CilentOrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ChenJunJie
 * @date 2022/6/18 17:23
 * 订单管理controller层
 */
@RestController
@RequestMapping(value = "/order", produces = "application/json;charset=utf-8")
public class CilentOrderController {

    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private CilentOrderService orderService;

    //提交订单(添加订单)
    @PostMapping("/submit")
    public JsonResult<String> addOrder(@RequestBody OrderAddDto orderAddDto, HttpServletRequest request) {

        //设置当前操作用户
        String token = request.getHeader("AuthorizationUser");
        String userId = redisTemplate.opsForValue().get(token);
        orderAddDto.setUserId(userId);

        boolean b = orderService.addOrder(orderAddDto);

        return b ? JsonResult.success("提交成功") : JsonResult.error("提交失败");
    }

    //移动端查询订单分页
    @GetMapping("/userPage")
    public JsonResult<OrderPagingVo> cilentorderPagination(@Param("page") Integer page, @Param("pageSize") Integer pageSize,HttpServletRequest request) {
        //设置当前操作用户
        String token = request.getHeader("AuthorizationUser");
        String userId = redisTemplate.opsForValue().get(token);

        OrderPagingVo orderPagingVo = orderService.cilentorderPagination(page, pageSize,userId);

        return JsonResult.employeePaginationSuccess(orderPagingVo, "查询成功");

    }
}
